package com.jp.study.model.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Range;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

import io.swagger.annotations.ApiModelProperty;

public class Paging implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = -8859054476799842586L;
    
    @ApiModelProperty(value = "当前页")
    @Max(value=9999999)
    @Min(value=0)
    private int pageIndex = 0;
    
    @ApiModelProperty(value = "每页条数")
    @Range(min=1,max=999999)
    private int pageSize = 10;
    
    @ApiModelProperty(value = "排序集合")
    @Valid
    private List<Ordering> sort = new ArrayList<>();
    
    public int getPageIndex() {
        return this.pageIndex;   
    }
    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }
    public int getPageSize() {
        return this.pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public List<Ordering> getSort() {
        return sort;
    }
    public void setSort(List<Ordering> sort) {
        this.sort = sort;
    }
    
    public Sort getSpringBootDataJpaSort(){
    	List<Order> orders=new ArrayList<>();
    	for (Ordering ordering :sort){
    		if(StringUtils.isNotEmpty(ordering.getDirection())&&StringUtils.isNotEmpty(ordering.getField())) {
    			orders.add(new Order(Direction.fromString(ordering.getDirection()),ordering.getField()));
			}
		}
    	return new Sort(orders);
    }
}

